package com.amigo.online.message.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.message.entity.MIMCJsonResponse;
import com.amigo.online.message.enums.MIMCUrlEnum;
import com.amigo.online.message.service.BlacklistService;
import com.amigo.online.message.util.MIMCHttpUtil;
import com.amigo.online.common.core.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class BlacklistServiceImpl implements BlacklistService{

    /**
     * 拉黑
     * @param userToken
     * @param userId
     * @return
     */
    public Result addBlacklist(String userToken,  String userId){

        ResponseEntity<MIMCJsonResponse> responseEntity = addBlacklistMessage(userToken, userId);
        if(responseEntity.getStatusCode().equals(HttpStatus.OK)){
            if(responseEntity.getBody().getCode() != 200){
                return Result.error();
            }else {
                return Result.success();
            }
        }else {
            return Result.error();
        }
    }
    /**
     * 取消拉黑
     * @param userToken
     * @param userId
     * @return
     */
    public Result deleteBlacklist(String userToken,  String userId){

        ResponseEntity<MIMCJsonResponse> responseEntity = deleteBlacklistMessage(userToken, userId);
        if(responseEntity.getStatusCode().equals(HttpStatus.OK)){
            if(responseEntity.getBody().getCode() != 200){
                return Result.error();
            }else {
                return Result.success();
            }
        }else {
            return Result.error();
        }
    }

    @Override
    public Result<Boolean> selectBlacklist(String userToken, String userId) {

        ResponseEntity<MIMCJsonResponse> responseEntity = selectBlacklistMessage(userToken, userId);
        if(responseEntity.getStatusCode().equals(HttpStatus.OK)){
            if(responseEntity.getBody().getCode() != 200){
                return Result.error();
            }else {
                return Result.success(responseEntity.getBody().getData().getBoolean("isBlack"));
            }
        }else {
            return Result.error();
        }
    }


    /**
     * 发送查看是否拉黑消息
     * @param userToken
     * @return
     */
    private ResponseEntity<MIMCJsonResponse> selectBlacklistMessage(String userToken,  String userId){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("blackAccount", userId);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(null, MIMCHttpUtil.getTokenHeaders(userToken));
        RestTemplate restTemplate =new RestTemplate();
        ResponseEntity<MIMCJsonResponse> responseEntity =  restTemplate.exchange(MIMCUrlEnum.SELECT_BLACKLIST.getValue()+userId, HttpMethod.GET, requestEntity , MIMCJsonResponse.class);
        log.debug("发送查看是否拉黑返回值: " + responseEntity.getBody());
        return responseEntity;
    }
    /**
     * 发送取消拉黑消息
     * @param userToken
     * @return
     */
    private ResponseEntity<MIMCJsonResponse> deleteBlacklistMessage(String userToken,  String userId){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("blackAccount", userId);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(null, MIMCHttpUtil.getTokenHeaders(userToken));
        RestTemplate restTemplate =new RestTemplate();
        ResponseEntity<MIMCJsonResponse> responseEntity =  restTemplate.exchange(MIMCUrlEnum.DELECT_BLACKLIST.getValue()+userId, HttpMethod.DELETE, requestEntity , MIMCJsonResponse.class);
        log.debug("解散群消息返回值: " + responseEntity.getBody());
        return responseEntity;
    }
    /**
     * 发送拉黑消息
     * @param userToken
     * @return
     */
    private ResponseEntity<MIMCJsonResponse> addBlacklistMessage(String userToken,  String userId){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("blackAccount", userId);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(jsonObject, MIMCHttpUtil.getTokenHeaders(userToken));
        RestTemplate restTemplate =new RestTemplate();
        ResponseEntity<MIMCJsonResponse> responseEntity =  restTemplate.exchange(MIMCUrlEnum.ADD_BLACKLIST.getValue(), HttpMethod.POST, requestEntity , MIMCJsonResponse.class);
        log.debug("解散群消息返回值: " + responseEntity.getBody());
        return responseEntity;
    }
}
